//
//  SearchDetailsViewController.m
//  aMoment
//
//  Created by dllo on 15/10/24.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "SearchDetailsViewController.h"
#import "AllHeaderFiles.h"

@interface SearchDetailsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataContentArray;
@property(nonatomic, retain)NSMutableArray *dataTopicArray;
@property(nonatomic, retain)NSArray *typeArray;
@property(nonatomic, retain)NSArray *sectionArray;
@end

@implementation SearchDetailsViewController

- (void)dealloc
{
    [_tableView release];
    [_dataContentArray release];
    [_dataTopicArray release];
    [_typeArray release];
    [_sectionArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.typeArray = [NSArray arrayWithObjects:@"content", @"topic", nil];
    self.sectionArray = [NSArray arrayWithObjects:@"故事", @"话题", nil];
    self.dataContentArray = [NSMutableArray array];
    self.dataTopicArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 2; i++) {
        [self getData:_typeArray[i]];
    }
}

#pragma mark 获取数据
- (void)getData:(NSString *)typeName
{
    [NetworkRequest getDataWithURLString:@"http://api2.pianke.me/search/get" BodyString:[NSString stringWithFormat:@"start=0&limit=10&%@&type=%@", _prevBodyString, typeName] DataBlock:^(id data) {
        NSDictionary *dataDict = [(NSDictionary *)data objectForKey:@"data"];
        NSArray *listArray = [NSArray arrayWithArray:[dataDict objectForKey:@"list"]];
        for (NSDictionary *dict in listArray) {
            SearchDetails *searchDetails = [[[SearchDetails alloc] init] autorelease];
            [searchDetails setValuesForKeysWithDictionary:dict];
            if ([typeName isEqualToString:@"content"]) {
                [_dataContentArray addObject:searchDetails];
            }
            if ([typeName isEqualToString:@"topic"]) {
                [_dataTopicArray addObject:searchDetails];
            }
        }
        [_tableView reloadData];
    }];
}

- (void)loadView
{
    [super loadView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    [_tableView release];
}

#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _dataContentArray.count;
    }
    else {
        return _dataTopicArray.count;
    }
}

#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:17];
    cell.detailTextLabel.numberOfLines = 0;
    //判断哪个section
    if (indexPath.section == 0) {
        cell.textLabel.text = [self.dataContentArray[indexPath.row] title];
        cell.detailTextLabel.text = [self.dataContentArray[indexPath.row] shortcontent];
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = [self.dataTopicArray[indexPath.row] title];
        cell.detailTextLabel.text = [self.dataTopicArray[indexPath.row] shortcontent];
    }
    return cell;
}

#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return [self getStringHeightWithString:[self.dataContentArray[indexPath.row] shortcontent] LabelWidth:SCREEN_WIDTH - 40] + 30;
    } else {
        return [self getStringHeightWithString:[self.dataTopicArray[indexPath.row] shortcontent] LabelWidth:SCREEN_WIDTH - 40] + 30;
    }
}

#pragma mark 获取字符串高度
- (CGFloat)getStringHeightWithString:(NSString *)string
                          LabelWidth:(CGFloat)labelWidth
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.height + 1;
}

#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        HomeDetailsViewController *contentVC = [[HomeDetailsViewController alloc] init];
        [self.navigationController pushViewController:contentVC animated:YES];
        contentVC.contentId = [_dataContentArray[indexPath.row] contentid];
        [contentVC release];
    }
    else {
        StoreDetailsViewController *topicVC = [[StoreDetailsViewController alloc] init];
        [self.navigationController pushViewController:topicVC animated:YES];
        topicVC.contentID = [_dataTopicArray[indexPath.row] contentid];
        [topicVC release];
    }
}

#pragma mark section个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark section标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return _sectionArray[section];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
